package cn.yunhe;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author wjh
 * @create 2024-04-21 14:58
 */
public class LoginApp extends JFrame{

    JPanel jp1,jp2,jp3;
    JTextField jtf1;
    JPasswordField jpf1;
    JLabel jlb1,jlb2;
    JButton jb1,jb2;

    //用于接下来的判断
    int TextLength = 10;
    int PasswordLength = 10;
    boolean start = false;
    
    static {
        
    }

    public LoginApp(){
        
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();

        jlb1 = new JLabel("用户名(10位)：");
        jlb2 = new JLabel("密码(10位)：");

        jb1 = new JButton("登录");
        jb2 = new JButton("退出");

        jb1.addActionListener(arg0 -> {

            if(jtf1.getText().trim().length()==0||new String(jpf1.getPassword()).trim().length()==0){
                //trim():用于删除头尾的空白符
                JOptionPane.showMessageDialog(null,"用户名密码不允许为空","message",JOptionPane.ERROR_MESSAGE);

            }
            else if(jtf1.getText().trim().length() > TextLength||new String(jpf1.getPassword()).trim().length() > PasswordLength){
                JOptionPane.showMessageDialog(null, "用户名或密码超出指定位数","message",JOptionPane.WARNING_MESSAGE);
            }
            else if(jtf1.getText().trim().equals("root")&&new String(jpf1.getPassword()).trim().equals("root")){
                JOptionPane.showMessageDialog(null, "登录成功");
                dispose();
                this.start = true;
            }
            else{
                JOptionPane.showMessageDialog(null, "用户名或密码错误","message",JOptionPane.QUESTION_MESSAGE);
            }
        });
        //设置监听
        jb2.addActionListener(e -> {
            dispose();
            //关闭窗口释放屏幕资源
        });
        
        //设计对话框及密码框的长度
        jtf1 = new JTextField(10);
        jpf1 = new JPasswordField(10);

        this.setLayout(new GridLayout(3,1));
        //采用网格布局三行一列

        //此JFrame加入三个JPanel
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);

        //第一个JPanel加入JLabel和JTextField
        jp1.add(jlb1);
        jp1.add(jtf1);

        //第二个JPanel加入JLabel和JPasswordField
        jp2.add(jlb2);
        jp2.add(jpf1);

        //第三个JPanel加入两个JButton
        jp3.add(jb1);
        jp3.add(jb2);

        this.setTitle("登录界面");
        this.setSize(411,184);
        //设置界面和虚拟机一起关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置界面可显示
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        
    }

    public static void main(String[] args) throws InterruptedException {
        SwingUtilities.invokeLater(() -> {
            new LoginApp();
        });

    }
}

